<?php
/**
 *
 * User: feikor
 * Date: 2023/06/12
 */

namespace app\admin\service;

use app\model\Drama;
use app\model\VideoCustom;
use app\model\VideoDetails;
use app\model\Video;
use app\model\VideoType;
use think\facade\Session;

class VideoService
{
	private $dramaModel;
	private $videoModel;
	private $videoTypeModel;
	private $videoDetailsModel;
	private $videoCustomModel;

	public function __construct
	(
		Video $videoModel,
		Drama $dramaModel,
		VideoCustom $videoCustomModel,
		VideoType $videoTypeModel,
		VideoDetails $videoDetailsModel
	)
	{
		$this->dramaModel = $dramaModel;
		$this->videoModel = $videoModel;
		$this->videoTypeModel = $videoTypeModel;
		$this->videoCustomModel = $videoCustomModel;
		$this->videoDetailsModel = $videoDetailsModel;
	}

	public function getDramaList($params = [])
	{
		$page = $params['page'] ?? 1;
		$limit = $params['limit'] ?? 100;
		$where = [];
		$data = $this->dramaModel->getPagingDataByWhere($where, $page, $limit);
		return $data;
	}

	// 获取视频视频列表
	public function getVideoDetailList($params = [])
	{
		$page = $params['page'] ?? 1;
		$limit = $params['limit'] ?? 100;
		$where = [];
		if (! empty($params['type'])) {
			$where[] = [
				'type_id',
				'=',
				$params['type']
			];
		}
		if (! empty($params['id'])) {
			$where[] = [
				'id',
				'=',
				$params['id']
			];
		}
		if (! empty($params['drama_id'])) {
			$where[] = [
				'drama_id',
				'=',
				$params['drama_id']
			];
		}
		if (isset($params['keywords']) && $params['keywords'] !== '') {
			$where[] = [
				'name|sort',
				'like',
				'%' . $params['keywords'] . '%'
			];
		}
		$data = $this->videoDetailsModel->getPagingDataByWhere($where, $page, $limit);
		return $data;
	}

	// 添加
	public function addVideo($params)
	{
		if (empty($params['filess']) || empty($params['filesname'])) {
			return [
				'msg'  => '请先操作开始上传后保存......!',
				'code' => 0
			];
		}
		$filess = json_decode($params['filess'], true);
		$filesname = json_decode($params['filesname'], true);
		$data = array_combine($filesname, $filess);
		if (isset($params['file_name']) && $params['file_name'] !== '') {
			$filename = $params['file_name'];
			$videos['name'] = $filename;
		} else {
			$filename = '';
		}
		$arr = [];
		$videos['number'] = $params['number'];
		foreach ($data as $key => $val) {
			if (! $filename) {
				$videos['name'] = $key;
			}
			$videos['url'] = $val;
			$videos['drama_id'] = $params['drama_id'];
			$videos['type_id'] = $params['type_id'];
			$videos['custom_id'] = $params['custom_id'];
			$videos['admin_id'] = Session::get('user_id');
			$videos['created_at'] = time();
			$arr[] = $videos;
		}
		$int = $this->videoDetailsModel->insertAll($arr);
		if (! $int) {
			return [
				'msg'  => '保存失败......!',
				'code' => 0
			];
		}
		return [
			'data' => $int,
			'msg'  => '保存成功',
			'code' => 1
		];
	}

	public function addDrama($params)
	{
		$id = $this->dramaModel->edit($params);
		if (! $id) {
			return false;
		}
		return true;
	}

	public function addCustom($params)
	{
		$id = $this->videoCustomModel->edit($params);
		if (! $id) {
			return false;
		}
		return true;
	}

	public function updateDrama($params)
	{
		$params[$params['field']] = $params['value'];
		unset($params['field'], $params['value']);
		$id = $this->dramaModel->edit($params);
		if (! $id) {
			return false;
		}
		return true;
	}

	public function batchSetLabel($params)
	{
		if (empty($params['ids'])) {
			return [
				'msg'  => '缺少参数ids......!',
				'code' => 0
			];
		}
		if (empty($params['type_id'])) {
			return [
				'msg'  => '请选择分类......!',
				'code' => 0
			];
		}
		$ids = json_decode($params['ids']);
		$idArr = explode(',', $ids[0]);
		$type_id = $params['type_id'];
		$arr = [];
		foreach ($idArr as $key => $id) {
			$arr[$key]['type_id'] = $type_id;
			$arr[$key]['id'] = $id;
			$arr[$key]['updated_at'] = time();
		}
		$int = $this->videoDetailsModel->saveAll($arr);
		if (! $int && $int !== 0) {
			return [
				'msg'  => '保存失败......!',
				'code' => 0
			];
		}
		return true;
	}

	public function deleteVideo($id)
	{
		$detail = $this->videoDetailsModel->where(['id' => $id])->find();
		$file_path = $_SERVER['DOCUMENT_ROOT'] . $detail['url'];// 测试文件的路径
		if (! file_exists($file_path)) {
			mkdir($file_path, 0777, true);
		}
		unlink($file_path);
		$bool = $this->videoDetailsModel->destroy($id);
		if (! $bool) {
			return false;
		}
		return true;
	}

	public function getAllType()
	{
		return $this->videoTypeModel->getAllData();
	}

	public function getTypeID()
	{
		return $this->videoTypeModel->getTypeID();
	}

	public function getAllDrama()
	{
		return $this->dramaModel->getAllData([], '*', 'id desc');
	}

	public function getAllCustom()
	{
		return $this->videoCustomModel->getAllData([], '*', 'id desc');
	}
}